package skills;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import main.Plugin;

public class SkillHandler implements CommandExecutor, Listener {
	private static HashMap<Player, HashMap<Skills, Skill>> playerSkills;

	public static Skill getSkill(Player player, Skills skill) {
		return playerSkills.get(player).get(skill);
	}
	
	public static Collection<Skill> getSkills(Player player) {
		return playerSkills.get(player).values();
	}

	private final Connection connection;

	public SkillHandler(Connection connection, Plugin plugin) {
		this.connection = connection;
		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
		playerSkills = new HashMap<Player, HashMap<Skills, Skill>>();
		for(Skills skill : Skills.values()) {
			plugin.getCommand(skill.name().toLowerCase()).setExecutor(this);
		}
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (sender instanceof Player) {
			Player player = (Player) sender;
			String skillName = command.getName().toLowerCase();
			Skills skill = Skills.getSkill(skillName);
			if (playerSkills.get(player).containsKey(skill)) {
				playerSkills.get(player).get(skill).info();
			}
		}
		return false;
	}

	@EventHandler
	private void playerJoin(PlayerJoinEvent event) {
		Player player = event.getPlayer();
		try {
			String query = "SELECT * FROM skills WHERE username = '" + player.getName() + "'";
			ResultSet data = connection.prepareStatement(query).executeQuery();
			HashMap<Skills, Skill> container = new HashMap<Skills, Skill>();
			if (data.next()) {
				for (Skills skill : Skills.values()) {
					String skillName = skill.name().toLowerCase();
					String[] info = data.getString(skillName).split(",");
					int level = Integer.parseInt(info[0]);
					int exp = Integer.parseInt(info[1]);
					container.put(skill, Skills.newSkill(skillName, player, level, exp));
				}
			} 
			else {
				try {
					String update = "INSERT INTO skills(username) VALUES ('" + player.getName() + "')";
					connection.prepareStatement(update).executeUpdate();
					query = "SELECT * FROM skills WHERE username = '" + player.getName() + "'";
					data = connection.prepareStatement(query).executeQuery();
					for (Skills skill : Skills.values()) {
						String skillName = skill.name().toLowerCase();
						container.put(skill, Skills.newSkill(skillName, player, 0, 0));
					}
				}
				catch (SQLException e) {
					Logger.getLogger("Minecraft").info("Unable to insert skills info for: " + player.getName());
				}
			}
			playerSkills.put(player, container);
		} 
		catch (SQLException e) {
			Logger.getLogger("Minecraft").info("Unable to query skills info for: " + player.getName());
		}
	}

	@EventHandler
	private void playerQuit(PlayerQuitEvent event) {
		Player player = event.getPlayer();
		for (Skill skill : playerSkills.get(player).values()) {
			String info = "'" + skill.level() + "," + skill.exp() + "'";
			try {
				String query = "UPDATE skills SET " + skill.name().toLowerCase() + "= " + info + " WHERE username = '" + player.getName() + "'";
				connection.prepareStatement(query).executeUpdate();
			} 
			catch (Exception e) {
				Logger.getLogger("Minecraft").info("Unable to update skills info for: "	+ player.getName());
			}
		}
	}
}
